﻿using System;
using System.Windows.Forms;
using MyFinanceManager.Controls;
using MyFinanceManager.Objects;

namespace MyFinanceManager
{
    public partial class NewRefuel : Form
    {
        private int step = -1;
        //private int amountOfSteps = 7;
        private int amountOfSteps = 6;

        ucAmount betrag;
        ucDate datum;
        ucTime uhrzeit;
        ucFuelstation tankstelle;
        ucMileometer kilometerstand;
        ucGasprice benzinpreis;
        //ucAccount konto;
        ucSave save;
        EventHandler<StringEventArg> stringEventHandler = delegate { };

        //PostingRecord datensatzA = new PostingRecord();
        FuelRecord datensatzF = new FuelRecord();

        public NewRefuel()
        {
            this.stringEventHandler = new EventHandler<StringEventArg>(item_NewValueAvailable);
            InitializeComponent();

            miVor_Click(this, null);
        }

        private void miZurück_Click(object sender, EventArgs e)
        {
            this.step--;
            ChangeUserControl();
        }

        private void miVor_Click(object sender, EventArgs e)
        {
            if (this.miVor.Text == "Exit")
            {
                this.Dispose();
            }
            else
            {
                this.step++;
                ChangeUserControl();
            }
        }

        private void ChangeUserControl()
        {
            SaveDateIfPossible();
            CreateIfNecessary();

            foreach (Control var in this.panel1.Controls)
            {
                var.Hide();
            }

            ShowRightUserControl();
        }

        private void ShowRightUserControl()
        {
            this.Refresh();

            this.miZurück.Enabled = !(step < 1);
            if (step > this.amountOfSteps)
            {
                this.miVor.Text = " ";

                this.laTitle.Text = "Speichere Eintrag";
                this.Refresh();

                try
                {
                    this.save.Value = "Versuche Datenbank zu initialisieren";
                    Database ds = Database.Db;
                    this.save.Value += "\r\nVersuch geglückt";

                    //this.save.Value += "\r\nFüge Eintrag der Abrechnung hinzu";
                    //ds.Expenses.Add(this.datensatzA);
                    this.save.Value += "\r\nFüge Eintrag der Benzinstatistik hinzu";
                    ds.Fuels.Add(this.datensatzF);
                    this.save.Value += "\r\nSpeichere Datenbank";
                    ds.Save();
                    this.save.Value += "\r\nAbgeschlossen";
                }
                catch (Exception ex)
                {
                    this.save.Value += "\r\nFehler:";
                    this.save.Value += "\r\n" + ex.Message;
                }

                this.laTitle.Text = "Eintrag gespeichert";
                this.miVor.Text = "Exit";
            }
            else
            {
                this.miVor.Text = "Vor";
                if (step == this.amountOfSteps)
                    this.miVor.Text = "Save";
            }

            switch (step)
            {
                case 0:
                    this.laTitle.Text = "Betrag eingeben";
                    this.betrag.Show();
                    break;
                case 1:
                    this.laTitle.Text = "Datum eingeben";
                    this.datum.Show();
                    break;
                case 2:
                    this.laTitle.Text = "Uhrzeit eingeben";
                    this.uhrzeit.Show();
                    break;
                case 3:
                    this.laTitle.Text = "Beschreibung eingeben";
                    this.tankstelle.Show();
                    break;
                case 4:
                    this.laTitle.Text = "Kilometerstand angeben";
                    this.kilometerstand.Show();
                    break;
                case 5:
                    this.laTitle.Text = "Benzinpreis angeben";
                    this.benzinpreis.Show();
                    break;
                //case 6:
                //    this.laTitle.Text = "Konto angeben";
                //    this.konto.Show();
                //    break;
                default:
                    this.laTitle.Text = "Speichere";
                    if (step == this.amountOfSteps)
                    {
                        //this.save.Value = datensatzA.ToString();
                        this.save.Value += datensatzF.ToString();
                    }
                    this.save.Show();
                    break;
            }
        }

        private void CreateIfNecessary()
        {
            switch (step)
            {
                case 0:
                    if (this.betrag == null)
                        this.betrag = new ucAmount();
                    this.panel1.Controls.Add(this.betrag);
                    break;
                case 1:
                    if (this.datum == null)
                        this.datum = new ucDate();
                    this.panel1.Controls.Add(this.datum);
                    break;
                case 2:
                    if (this.uhrzeit == null)
                        this.uhrzeit = new ucTime();
                    this.panel1.Controls.Add(this.uhrzeit);
                    break;
                case 3:
                    if (this.tankstelle == null)
                        this.tankstelle = new ucFuelstation();
                    this.panel1.Controls.Add(this.tankstelle);
                    break;
                case 4:
                    if (this.kilometerstand == null)
                        this.kilometerstand = new ucMileometer();
                    this.panel1.Controls.Add(this.kilometerstand);
                    break;
                case 5:
                    if (this.benzinpreis == null)
                        this.benzinpreis = new ucGasprice();
                    this.panel1.Controls.Add(this.benzinpreis);
                    break;
                //case 6:
                //    if (this.konto == null)
                //        this.konto = new ucAccount();
                //    this.panel1.Controls.Add(this.konto);
                //    break;
                case 6:
                    if (this.save == null)
                        this.save = new ucSave();
                    this.panel1.Controls.Add(this.save);
                    break;
                default:
                    break;
            }

            foreach (FiringUserControl item in this.panel1.Controls)
            {
                item.NewValueAvailable -= this.stringEventHandler;
                item.NewValueAvailable += this.stringEventHandler;
            }
        }

        void item_NewValueAvailable(object sender, StringEventArg e)
        {
            this.laTitle.Text = "Gewählt: " + e.Info;
        }

        /// <summary>
        /// Saves the date if possible.
        /// </summary>
        private void SaveDateIfPossible()
        {
            if (this.betrag != null)
            {
                //this.datensatzA.Amount = this.betrag.Value;
                this.datensatzF.Amount = this.betrag.Value;
            }

            if (this.datum != null)
            {
                //this.datensatzA.Time = this.datum.Value;
                this.datensatzF.Time = this.datum.Value;
            }

            if (this.uhrzeit != null)
            {
                this.datensatzF.Time = new DateTime(this.datensatzF.Time.Year,
                    this.datensatzF.Time.Month,
                    this.datensatzF.Time.Day,
                    this.uhrzeit.Value.Hour,
                    this.uhrzeit.Value.Minute,
                    0);
                //this.datensatzA.Time = this.datensatzF.Time;
            }
            if (this.tankstelle != null && !String.IsNullOrEmpty(this.tankstelle.Value))
            {
                //this.datensatzA.Description = "getankt bei " + this.tankstelle.Value;
                this.datensatzF.Fuelstation = this.tankstelle.Value;
            }

            //if (this.konto != null)
            //    this.datensatzA.AccountName = this.konto.Value;

            //this.datensatzA.SubCategoryName = "Benzin";
            //this.datensatzA.CategoryName = "Auto";

            if (this.kilometerstand != null)
                this.datensatzF.Mileage = this.kilometerstand.Value;

            if (this.benzinpreis != null)
                this.datensatzF.Gasprice = this.benzinpreis.Value;

            if (this.datensatzF.Gasprice > 0)
            {
                this.datensatzF.AmountOfGas = this.datensatzF.Amount / this.datensatzF.Gasprice;
            }
        }
    }
}